// pages/mine-fabu/mine-fabu.js
const myRequest = require("../../utils/require.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      status:[
        '我发布的',
        '已审核',
        '审核中',
        '审核失败'
      ],
    status_index:'0',
    list: [],
    next: true,
    page: 1,
    search_key: '',
    more_title: '点击加载更多厂房...',
    status:'',
    time: '',
    statusList: [
      '已审核',
      '正在审核中',
      '审核失败',
    ],
    shenhe:'',
  },
  Search(e) {
    console.log(e)
    this.setData({
      search_key: e.detail.value
    })
  },
  Sousuo(e) {
    this.setData({
      list: [],
      page: 1,
      next: true
    })
    this.apiRequset()
  },
  NavChange(e) {
    this.setData({
      shenhe: e.currentTarget.dataset.cur,
      page:1,
      list:[],
      next:true
    });
    this.apiRequset();
  },
  apiRequset() {
    if (this.data.next == null) {
      return
    }
    myRequest.myRequest('DisSource/publishList', { page: this.data.page, time:this.data.time,search_key: this.data.search_key,shenhe:this.data.shenhe }, 'GET').then(list => {
      console.log(list)
      let page = this.data.page
      let data = this.data.list
      data = data.concat(list.data)
      page++
      this.setData({
        list: data,
        page: page,
        next: list.links.next
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    app.CheckSession().then(res => {
      if (res == true) {
        app.GetUser().then(data => {
          
          let time=''
          if(options.time!=undefined){
              time=options.time
          }
          let shenhe=''
     
          if (options.shenhe != undefined) {
            shenhe = options.shenhe
              
          }
          this.setData({
            userInfo: data,
            time:time,
            shenhe:shenhe
          })
          this.apiRequset()
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.apiRequset()
  },

  ChangePublish(e){
    console.log(e)
    this.setData({
      status_index:e.currentTarget.dataset.index
    })
  },

  loadpage: function () {
    console.error("this.data.workshop_next")

    // 显示加载图标
    this.setData({
      show: true
    })
    let status = this.data.status
    let page = this.data.page
    let workshops = this.data.workshop
    wx.request({
      url: 'http://127.0.0.1:8000/agentApi/' + "workshop_fabu",
      method: "GET",
      data: {
        page: page,
        status: status,
      },
      success: res => {
        if (res.data.code == 0) {
          page++;
          this.setData({
            page: page
          })
          for (let i = 0; i < res.data.data.length; i++) {
            workshops.push(res.data.data[i])
          }
          this.setData({

            workshop: workshops,
            workshop_next: res.data.links.next,
            workshop_total: res.data.meta.total
          })
          if (res.data.links.next == undefined || res.data.links.next === null) {
            this.setData({
              more_title: '没有更多数据了'
            })

          } else {
            this.setData({
              more_title: '点击加载更多厂房...'
            })
          }
        }
        this.setData({
          show: false,
          pull: false
        })
      }
    })
   
  }
})
